import { getToken, setToken, removeToken } from "@/utils/auth";
import { login, getUserInfo } from "@/api/user";
const state = {
  token: getToken(),
  userInfo: {} // 存储用户基本资料状态
};
const mutations = {
  setToken(state, token) {
    state.token = token;
    setToken(token);
  },
  removeToken(state) {
    state.token = null;
    removeToken();
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
  }
};
const actions = {
  // 登录
  async login(context, data) {
    const token = await login(data)
    context.commit("setToken", token);
  },
  //获取用户的基本资料
  async getUserInfo(context) {
    const result = await getUserInfo()
    context.commit('setUserInfo', result)
  },
  // 退出登录的action
  logout(context) {
    // 删除token
    context.commit('removeToken') 
    // 设置用户信息为空对象
    context.commit('setUserInfo', {})
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
